package com.niiwoo.civet.trade.service.local.contract;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.dao.entity.ProjectContract;
import com.niiwoo.civet.trade.enums.FadadaContractEnum;
import com.niiwoo.tripod.fadada.FadadaService;
import com.niiwoo.tripod.fadada.dto.request.ExtSignRequestDTO;
import com.niiwoo.tripod.fadada.dto.request.GenerateContractRequestDTO;
import com.niiwoo.tripod.fadada.dto.response.FadadaResponseDTO;
import com.niiwoo.tripod.fadada.exception.FadadaException;
import com.niiwoo.tripod.provider.exception.BizException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.ExecutionException;

public abstract class AbstractFadadaContract implements FadadaContract {
	
	private static final Logger log = LoggerFactory.getLogger(AbstractFadadaContract.class);

	@Autowired
	private FadadaService fadadaService;

	protected FadadaResponseDTO generateContract(GenerateContractRequestDTO dto) throws Exception {
		FadadaResponseDTO result = null;
		try {
			result = fadadaService.generateContract(dto).get();
			log.info("generateContract ======>>>>" + JSON.toJSON(result));
		} catch (InterruptedException e) {
			log.error("",e);
			throw new Exception(e);
		} catch (ExecutionException e) {
			log.error("",e);
			if (e.getCause() instanceof FadadaException) {
				FadadaException exception = (FadadaException) e.getCause();

				log.error("generateContract : ", exception);
                throw new BizException("", exception.getMessage());
			} else {
				throw new Exception(e);
			}
		}
		return result;
	}

	protected FadadaResponseDTO extSignAuto(ExtSignRequestDTO extSignRequestDTO) throws Exception{
		FadadaResponseDTO result = null;
		try {
			result = fadadaService.extSignAuto(extSignRequestDTO).get();
			log.info("extSignAuto ======>>>>" + JSON.toJSON(result));
		} catch (InterruptedException e) {
			log.error("",e);
			throw new Exception(e);
		} catch (ExecutionException e) {
			log.error("",e);
			if (e.getCause() instanceof FadadaException) {
				FadadaException exception = (FadadaException) e.getCause();

				log.error("generateContract : ", exception);
                throw new BizException("", exception.getMessage());
			} else {
				throw new Exception(e);
			}
		}
		return result;
	}

	protected FadadaResponseDTO contractFilling(String contractId) throws Exception{
		FadadaResponseDTO result = null;
		try {
			result = fadadaService.contractFilling(contractId).get();
			log.info("contractFilling ======>>>>" + JSON.toJSON(result));
		} catch (InterruptedException e) {
			log.error("",e);
			throw new Exception(e);
		} catch (ExecutionException e) {
			log.error("",e);
			if (e.getCause() instanceof FadadaException) {
				FadadaException exception = (FadadaException) e.getCause();

				log.error("generateContract : ", exception);
                throw new BizException("", exception.getMessage());
			} else {
				throw new Exception(e);
			}
		}
		return result;
	}

	protected FadadaResponseDTO extSignAuto(String transactionId, String contractId, String caCertId,
			String clientRole, String docTitle, String signKeyword) throws Exception{
		ExtSignRequestDTO extSignRequestDTO = new ExtSignRequestDTO();
		extSignRequestDTO.setTransactionId(transactionId);
		extSignRequestDTO.setContractId(contractId);
		extSignRequestDTO.setCustomerId(caCertId);
		extSignRequestDTO.setClientRole(clientRole);
		extSignRequestDTO.setDocTitle(docTitle);
		extSignRequestDTO.setSignKeyword(signKeyword);
		return extSignAuto(extSignRequestDTO);
	}
	
	protected ProjectContract buildContractUpdateRecord(String templateId, String contractNo, String transactionId,
			String viewUrl, String downloadUrl) {
		ProjectContract projectContract = new ProjectContract();
		projectContract.setTemplateId(templateId);
		projectContract.setContractNo(contractNo);
		projectContract.setTransactionId(transactionId);
		projectContract.setStatus(FadadaContractEnum.StatusEnum.YES.getStatus());
		projectContract.setViewUrl(viewUrl);
		projectContract.setDownloadUrl(downloadUrl);

		return projectContract;
	}

}
